package com.song.tools.songtest.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * Snowflake 配置
 *
 * @author song tools
 * @since 2024-07-15
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(SnowflakeProperties.class)
public class SnowflakeConfiguration {

    private final SnowflakeProperties snowflakeProperties;

    public SnowflakeConfiguration(@Autowired SnowflakeProperties snowflakeProperties) {
        this.snowflakeProperties = snowflakeProperties;
    }

    @Bean
    @Primary
    public DistributedSnowflake snowflake() {
        log.info("生成Snowflake实例，workerId={}, dataCenterId={}", snowflakeProperties.getWorkerId(), snowflakeProperties.getDataCenterId());
        return new DistributedSnowflake(snowflakeProperties.getWorkerId(), snowflakeProperties.getDataCenterId());
    }
}
